//package com.starblink.android.basic.sensorsdata.exposure
//
//import android.graphics.Rect
//import android.text.TextUtils
//import android.util.Log
//import android.view.View
//import androidx.recyclerview.widget.GridLayoutManager
//import androidx.recyclerview.widget.LinearLayoutManager
//import androidx.recyclerview.widget.RecyclerView
//import androidx.recyclerview.widget.StaggeredGridLayoutManager
//import com.starblink.basic.autosize.AutoSizeConfig
//import java.lang.Exception
//import java.util.concurrent.ConcurrentHashMap
//
//internal class ViewShowCountUtils {
//    //刚进入列表时统计当前屏幕可见views
//    private var isFirstVisible = true
//
//    //用于统计曝光量的map
//    private val hashMap = ConcurrentHashMap<String, Int>()
//
//    /*
//     * 统计RecyclerView里当前屏幕可见子view的曝光量
//     *
//     * */
//    fun recordViewShowCount(recyclerView: RecyclerView?) {
//        hashMap.clear()
//        if (recyclerView == null || recyclerView.visibility != View.VISIBLE) {
//            return
//        }
//        //检测recylerview的滚动事件
//        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
//            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
//                /*
//                我这里通过的是停止滚动后屏幕上可见view。如果滚动过程中的可见view也要统计，你可以根据newState去做区分
//                SCROLL_STATE_IDLE:停止滚动
//                SCROLL_STATE_DRAGGING: 用户慢慢拖动
//                SCROLL_STATE_SETTLING：惯性滚动
//                */
//                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                    getVisibleViews(recyclerView)
//                }
//            }
//
//            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
//                super.onScrolled(recyclerView, dx, dy)
//                //刚进入列表时统计当前屏幕可见views
//                if (isFirstVisible) {
//                    getVisibleViews(recyclerView)
//                    isFirstVisible = false
//                }
//            }
//        })
//    }
//
//    /*
//     * 获取当前屏幕上可见的view
//     * */
//    private fun getVisibleViews(reView: RecyclerView?) {
//        if (reView == null || reView.visibility != View.VISIBLE ||
//            !reView.isShown || !reView.getGlobalVisibleRect(Rect())
//        ) {
//            return
//        }
//        //保险起见，为了不让统计影响正常业务，这里做下try-catch
//        try {
//            var range = IntArray(2)
//            val manager = reView.layoutManager
//            if (manager is LinearLayoutManager) {
//                range = findRangeLinear(manager)
//            } else if (manager is GridLayoutManager) {
//                range = findRangeGrid(manager)
//            } else if (manager is StaggeredGridLayoutManager) {
//                range = findRangeStaggeredGrid(manager)
//            }
//            if (range == null || range.size < 2) {
//                return
//            }
//            Log.i("qcl0402", "屏幕内可见条目的起始位置：" + range[0] + "---" + range[1])
//            for (i in range[0]..range[1]) {
//                val view = manager!!.findViewByPosition(i)
//                recordViewCount(view)
//            }
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
//    }
//
//    //获取view绑定的数据
//    private fun recordViewCount(view: View?) {
//        if (view == null || view.visibility != View.VISIBLE ||
//            !view.isShown || !view.getGlobalVisibleRect(Rect())
//        ) {
//            return
//        }
//        val top = view.top
//        val halfHeight = view.height / 2
//        val screenHeight = AutoSizeConfig.getInstance().screenHeight
//        //      int statusBarHeight = UiUtils.getStatusBarHeight(view.getContext());
//        if (top < 0 && Math.abs(top) > halfHeight) {
//            return
//        }
//        //      if (top > screenHeight - halfHeight - statusBarHeight) {
//        if (top > screenHeight - halfHeight) {
//            return
//        }
//
//        //这里获取的是我们view绑定的数据，相应的你要去在你的view里setTag，只有set了，才能get
////      ItemData tag = (ItemData) view.getTag();
////      String key = tag.toString();
//        val key = ""
//        if (TextUtils.isEmpty(key)) {
//            return
//        }
//        hashMap[key] = if (!hashMap.containsKey(key)) 1 else hashMap[key]!! + 1
//        Log.i("qcl0402", key + "----出现次数：" + hashMap[key])
//    }
//
//    private fun findRangeLinear(manager: LinearLayoutManager): IntArray {
//        val range = IntArray(2)
//        range[0] = manager.findFirstVisibleItemPosition()
//        range[1] = manager.findLastVisibleItemPosition()
//        return range
//    }
//
//    private fun findRangeGrid(manager: GridLayoutManager): IntArray {
//        val range = IntArray(2)
//        range[0] = manager.findFirstVisibleItemPosition()
//        range[1] = manager.findLastVisibleItemPosition()
//        return range
//    }
//
//    private fun findRangeStaggeredGrid(manager: StaggeredGridLayoutManager): IntArray {
//        val startPos = IntArray(manager.spanCount)
//        val endPos = IntArray(manager.spanCount)
//        manager.findFirstVisibleItemPositions(startPos)
//        manager.findLastVisibleItemPositions(endPos)
//        return findRange(startPos, endPos)
//    }
//
//    private fun findRange(startPos: IntArray, endPos: IntArray): IntArray {
//        var start = startPos[0]
//        var end = endPos[0]
//        for (i in 1 until startPos.size) {
//            if (start > startPos[i]) {
//                start = startPos[i]
//            }
//        }
//        for (i in 1 until endPos.size) {
//            if (end < endPos[i]) {
//                end = endPos[i]
//            }
//        }
//        return intArrayOf(start, end)
//    }
//}